package com.leoman.spinningmachine.tab1;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Toast;

import com.leoman.helper.util.LogUtil;
import com.leoman.helper.util.ToastUtil;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.base.BaseFragment;
import com.leoman.spinningmachine.bean.QrcodeResultBean;
import com.leoman.spinningmachine.http.APIResponse;
import com.leoman.spinningmachine.http.WebServiceApi;
import com.leoman.spinningmachine.util.SharedPreferencesUtils;

import butterknife.BindView;
import cn.bingoogolapple.qrcode.core.BGAQRCodeUtil;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;

/**
 * 扫码
 */
public class SweepFragment2 extends BaseFragment implements QRCodeView.Delegate {

    @BindView(R.id.zxingview)
    ZXingView mZXingView;

    private String token;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fg_sweep2;
    }

    @Override
    protected void initView(View view) {
        requestPermission();
        token = SharedPreferencesUtils.getInstance().getString("token");
        mZXingView.setDelegate(this);
    }

    @Override
    protected void initData() {
        BGAQRCodeUtil.setDebug(false);
    }

    @Override
    public void onStart() {
        super.onStart();
        if(mZXingView!=null){
            mZXingView.startCamera();
            mZXingView.startSpotAndShowRect();
        }
    }

    @Override
    public void onStop() {
        if(mZXingView!=null) {
            mZXingView.stopCamera();
        }
        super.onStop();
    }

    @Override
    public void onDestroy() {
        if(mZXingView!=null){
            mZXingView.onDestroy();
        }
        super.onDestroy();
    }

    /**
     * 获取mac和时间搓
     * @param resultStr
     */
    private void openDoor(String resultStr) {
        WebServiceApi.getInstance().qrCodeOpenDoor(token,resultStr,this,1);
    }

    /**
     * 重新扫描
     */
    private void reScan() {
        if(mZXingView!=null){
            mZXingView.startSpotAndShowRect();
        }
    }

    @Override
    public void onScanQRCodeSuccess(String s) {
        mZXingView.startSpot(); // 延迟0.5秒后开始识别
        mZXingView.stopCamera();
        mZXingView.stopSpot(); // 停止识别
        mZXingView.hiddenScanRect();
        LogUtil.json("resultStr:"+s);
        openDoor(s);
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        LogUtil.json("识别失败");
        ToastUtil.showToast(getActivity(),"二维码识别失败!",Toast.LENGTH_LONG);
    }

    @Override
    public void OnSuccessData(APIResponse apiResponse, Integer tag) {
        super.OnSuccessData(apiResponse, tag);
        QrcodeResultBean bean = apiResponse.data.result;
        if(bean!=null){
            if(1 == bean.getStatus()){
                ToastUtil.showToast(getActivity(),"开门成功！",Toast.LENGTH_SHORT);
            }else{
                ToastUtil.showToast(getActivity(),"开门失败，重新扫描！",Toast.LENGTH_SHORT);
                reScan();
            }
        }
    }

    @Override
    public void OnFailureData(String error, Integer tag) {
        super.OnFailureData(error, tag);
        reScan();
    }

    @Override
    public void OnErrorData(String code, Integer tag) {
        super.OnErrorData(code, tag);
        reScan();
    }


    private void requestPermission() {
        if (Build.VERSION.SDK_INT >= 23 ) {
            int checkCallPhonePermission = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA);
            if(checkCallPhonePermission != PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE},100);
                return;
            }else{

            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 100){
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                LogUtil.json("tongyi ");

            }else{
                LogUtil.json("butongyi");
            }
        }
    }
}
